package com.bulletelc.apollopre.module.main.center.setting;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.constants.GlobalConfig;
import com.bulletelc.apollopre.constants.IntentCode;
import com.bulletelc.apollopre.entity.SettingBean;
import com.bulletelc.apollopre.utils.CleanUtil;

import java.util.List;

/**
 * Created by CX on 2018/3/30.
 */

public class SettingAdapter extends BaseAdapter {

    private List<SettingBean> settingBeans;
    private Context context;
    private LayoutInflater inflater;

    private onCheckPermissionListener listener;

    public SettingAdapter(Context context, List<SettingBean> settingBeans) {
        this.context = context;
        this.settingBeans = settingBeans;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return settingBeans.size();
    }

    @Override
    public Object getItem(int i) {
        if (settingBeans.size() ==0) {
            return 0;
        } else {
            return settingBeans.get(i);
        }
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final int pos = i;
        ViewHolder holderView;

        view = inflater.inflate(R.layout.setting_list_item, null, false);
        holderView = new ViewHolder();
        holderView.llItem = view.findViewById(R.id.llItem);
        holderView.tvTitle = view.findViewById(R.id.tvTitle);
        view.setTag(holderView);

        holderView.tvTitle.setText(settingBeans.get(i).getTitle());
        holderView.llItem.setOnClickListener(new ItemListener());
        holderView.llItem.setTag(i);
        return view;
    }

    class ItemListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            int position = (int) view.getTag();
            int flag = settingBeans.get(position).getActionFlag();
            switch (flag) {
                case GlobalConfig.MSM_ACTION_MODIFY_MOBILE:
                    Intent intent = new Intent(context, settingBeans.get(position).getClassName());
                    intent.putExtra(IntentCode.EXTRA_KEY_SETTING_ACTION, GlobalConfig.MSM_ACTION_MODIFY_MOBILE);
                    context.startActivity(intent);
                    break;
                case GlobalConfig.MSM_ACTION_MODIFY_PASSWORD:
                    Intent intent2 = new Intent(context, settingBeans.get(position).getClassName());
                    intent2.putExtra(IntentCode.EXTRA_KEY_SETTING_ACTION, GlobalConfig.MSM_ACTION_MODIFY_PASSWORD);
                    context.startActivity(intent2);
                    break;
                case GlobalConfig.SETTING_MODIFY_INFO:
                    Intent intent3 = new Intent(context, settingBeans.get(position).getClassName());
                    context.startActivity(intent3);
                    break;
                case GlobalConfig.SETTING_CLEAR_CACHE:
                    CleanUtil.cleanAllCache();
                    Toast.makeText(context, "清理成功", Toast.LENGTH_SHORT).show();
                    break;
                case GlobalConfig.SETTING_CHECK_VERSION:
                    if (listener != null) {
                        listener.check();
                    }
                    break;
            }
        }
    }

    static class ViewHolder {
        TextView tvTitle;
        LinearLayout llItem;
    }

    public void setOnCheckPermissionListener(onCheckPermissionListener listener) {
        this.listener = listener;
    }

    interface onCheckPermissionListener {
        void check();
    }
}
